﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;
using AjaxPro;

public partial class backstage_goods_add : System.Web.UI.Page
{
    // Fields
    protected AttributesManager am = new AttributesManager();
   
    protected CategoryManager cm = new CategoryManager();
  
    protected FreightTemplateManager ftm = new FreightTemplateManager();
    protected GoodsAttributeManager gam = new GoodsAttributeManager();
    protected GoodsCategory gc = new GoodsCategory();
    protected GoodsCategoryManager gcm = new GoodsCategoryManager();
    protected GoodsEntitysManager gem = new GoodsEntitysManager();
    protected GoodsEntitysSpecificationsManager gesm = new GoodsEntitysSpecificationsManager();
    protected GoodGalleryManager ggm = new GoodGalleryManager();
    protected GoodsManager gm = new GoodsManager();
    protected Goods gs = new Goods();
    protected GoodsTypeManager gtm = new GoodsTypeManager();

    protected SpecificationsManager sm = new SpecificationsManager();

    // Methods
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            this.gs.CategoryId = base.Request.Form["addCategory"];
            this.gs.Brand = Convert.ToInt32(base.Request.Form["brand"]);
            this.gs.GoodsType = Convert.ToInt32(base.Request.Form["sGoodsType"]);
            this.gs.GoodsName = this.txtGoodsName.Value;
            this.gs.GoodsNo = this.txtGoodsNo.Value;
            this.gs.Origin = this.txtOrigin.Value;
            this.gs.IsBest = Convert.ToInt32(base.Request.Form["isBest"]);
            this.gs.IsNew = Convert.ToInt32(base.Request.Form["isNew"]);
            this.gs.IsTopSelling = Convert.ToInt32(base.Request.Form["isTopSelling"]);
            this.gs.IsShelves = Convert.ToInt32(base.Request.Form["isShelves"]);
            this.gs.IsPromote = Convert.ToInt32(base.Request.Form["isPromote"]);
            this.gs.IsUserDiscount = Convert.ToInt32(base.Request.Form["isUserDiscount"]);
            this.gs.IsOrderFromFreight = this.cbIsOrderFromFreight.Checked ? 1 : 0;
            this.gs.IsDiscount = this.cbIsDiscount.Checked ? 1 : 0;
            this.gs.IsTotalReduce = this.cbIsTotalReduce.Checked ? 1 : 0;
            this.gs.IsGifts = this.cbIsGifts.Checked ? 1 : 0;
            this.gs.IsSendIntegral = this.cbIsSendIntegral.Checked ? 1 : 0;
            this.gs.IsFromFreight = Convert.ToInt32(base.Request.Form["isFromFreight"]);
            this.gs.FreightTemplate = (this.gs.IsFromFreight == 1) ? 0 : Convert.ToInt32(base.Request.Form["freightTemplate"]);
            this.gs.Keywords = this.txtKeywords.Value;
            this.gs.Description = this.txtaDescription.Value;
            Admin admin = this.Session["admin_user"] as Admin;
            this.gs.Creator = admin.Id;
            this.gs.LanguageVersion = this.Session["language_version"].ToString();
            this.gs.GoodsRemark = this.fckGoodsRemark.Value;
            this.gs.Id = this.gm.AddGoods(this.gs);
            if (this.gs.Id != 0)
            {
                int num;
                Array array = this.gs.CategoryId.Split(new char[] { ',' });
                foreach (string str in array)
                {
                    this.gc.Goods = this.gs.Id;
                    this.gc.Category = Convert.ToInt32(str);
                    this.gcm.AddGoodsCategory(this.gc);
                }
                ArrayList list = ArrayList.Adapter(base.Request.Form["barCode"].Split(new char[] { ',' }));
                ArrayList list2 = ArrayList.Adapter(base.Request.Form["goodsArtno"].Split(new char[] { ',' }));
                ArrayList list3 = ArrayList.Adapter(base.Request.Form["salePrice"].Split(new char[] { ',' }));
                ArrayList list4 = ArrayList.Adapter(base.Request.Form["costPrice"].Split(new char[] { ',' }));
                ArrayList list5 = ArrayList.Adapter(base.Request.Form["marketPrice"].Split(new char[] { ',' }));
                ArrayList list6 = ArrayList.Adapter(base.Request.Form["giveIntegral"].Split(new char[] { ',' }));
                ArrayList list7 = ArrayList.Adapter(base.Request.Form["goodsWeight"].Split(new char[] { ',' }));
                ArrayList list8 = ArrayList.Adapter(base.Request.Form["selectWeightUnit"].Split(new char[] { ',' }));
                ArrayList list9 = ArrayList.Adapter(base.Request.Form["inventory"].Split(new char[] { ',' }));
                for (num = 0; num < list2.Count; num++)
                {
                    GoodsEntitys goodsEntitys = new GoodsEntitys();
                    goodsEntitys.Goods = this.gs.Id;
                    goodsEntitys.BarCode = list[num].ToString();
                    goodsEntitys.GoodsArtno = list2[num].ToString();
                    goodsEntitys.SalePrice = Convert.ToDecimal(list3[num]);
                    goodsEntitys.CostPrice = Convert.ToDecimal(list4[num]);
                    goodsEntitys.MarketPrice = Convert.ToDecimal(list5[num]);
                    goodsEntitys.Integral = 0;
                    goodsEntitys.GiveIntegral = Convert.ToInt32(list6[num]);
                    goodsEntitys.GoodsWeight = Convert.ToDecimal(list7[num]);
                    goodsEntitys.WeightUnit = list8[num].ToString();
                    goodsEntitys.Inventory = Convert.ToInt32(list9[num]);
                    goodsEntitys.Id = this.gem.AddGoodsEntitys(goodsEntitys);
                    if (goodsEntitys.Id != 0)
                    {
                        GoodsEntitysSpecifications ges = new GoodsEntitysSpecifications();
                        DataTable specificationsByLanguageVersion = this.sm.GetSpecificationsByLanguageVersion(this.Session["language_version"].ToString());
                        foreach (DataRow row in specificationsByLanguageVersion.Rows)
                        {
                            if (base.Request.Form["SpecificationsValue" + row["id"].ToString() + ""] != null)
                            {
                                ges.GoodsEntitys = goodsEntitys.Id;
                                ges.Specifications = Convert.ToInt32(row["id"]);
                            //    ges.SpecificationsValue = Convert.ToInt32(ArrayList.Adapter(base.Request.Form["SpecificationsValue" + row["id"].ToString() + ""].Split(new char[] { 0x2c }))[num]);
                                this.gesm.AddGoodsEntitysSpecifications(ges);
                            }
                        }
                    }
                }
                HttpFileCollection files = HttpContext.Current.Request.Files;
                ArrayList list10 = ArrayList.Adapter(base.Request.Form["imgAlt"].Split(new char[] { ',' }));
                for (num = 0; num < files.Count; num++)
                {
                    GoodGallery goodGallery = new GoodGallery();
                    goodGallery.Goods = this.gs.Id;
                    string str2 = DateTime.Now.ToString("yyyyMMddHHssffffff");
                    goodGallery.BigImg = "/cn/goods/big/" + str2 + Path.GetExtension(files[num].FileName);
                    goodGallery.SmallImg = "/cn/goods/small/" + str2 + Path.GetExtension(files[num].FileName);
                    goodGallery.ThumbImg = "/cn/goods/thumb/" + str2 + Path.GetExtension(files[num].FileName);
                    files[num].SaveAs(base.Server.MapPath(goodGallery.BigImg));
                    ThumbnailImage image = new ThumbnailImage();
                    image.GetThumbnail(base.Server.MapPath(goodGallery.BigImg), base.Server.MapPath(goodGallery.SmallImg), 500, 500, 1, Color.White);
                    image.GetThumbnail(base.Server.MapPath(goodGallery.BigImg), base.Server.MapPath(goodGallery.ThumbImg), 200, 200, 1, Color.White);
                    goodGallery.ImgAlt = list10[num].ToString();
                    this.ggm.AddGoodGallery(goodGallery);
                }
                DataTable atrributesByGoodsTypeAndState = this.am.GetAtrributesByGoodsTypeAndState(this.gs.GoodsType, 1);
                int num2 = 0;
                if (atrributesByGoodsTypeAndState.Rows.Count > 0)
                {
                    ArrayList list11 = new ArrayList();
                    if (base.Request.Form["atrributesValue"] != null)
                    {
                        list11 = ArrayList.Adapter(base.Request.Form["atrributesValue"].Split(new char[] { ',' }));
                    }
                    for (int i = 0; i < atrributesByGoodsTypeAndState.Rows.Count; i++)
                    {
                        GoodsAttribute goodsAttribute = new GoodsAttribute();
                        goodsAttribute.Goods = this.gs.Id;
                        goodsAttribute.Attributes = Convert.ToInt32(atrributesByGoodsTypeAndState.Rows[i]["id"]);
                        switch (Convert.ToInt32(atrributesByGoodsTypeAndState.Rows[i]["attributeInputType"]))
                        {
                            case 0:
                            case 1:
                            case 2:
                            case 5:
                                goodsAttribute.InputValue = list11[num2].ToString();
                                goodsAttribute.AttributeValue = "0";
                                this.gam.AddGoodsAttribute(goodsAttribute);
                                num2++;
                                break;

                            case 3:
                                {
                                    string str3 = "rdAtrributes" + atrributesByGoodsTypeAndState.Rows[i]["id"].ToString();
                                    goodsAttribute.AttributeValue = base.Request.Form[str3];
                                    goodsAttribute.InputValue = "";
                                    this.gam.AddGoodsAttribute(goodsAttribute);
                                    break;
                                }
                            case 4:
                                {
                                    string str4 = "cbAtrributes" + atrributesByGoodsTypeAndState.Rows[i]["id"].ToString();
                                    goodsAttribute.AttributeValue = base.Request.Form[str4];
                                    goodsAttribute.InputValue = "";
                                    this.gam.AddGoodsAttribute(goodsAttribute);
                                    break;
                                }
                        }
                    }
                }
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"goods_list.aspx\";</script>");
            }
            base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
        }
        catch
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加失败！\");</script>");
        }
    }

    protected StringBuilder CategoryBind()
    {
        StringBuilder sb = new StringBuilder();
        try
        {
            this.RecursionCategory(sb, "00000000-0000-0000-0000-000000000000", this.Session["language_version"].ToString());
        }
        catch
        {
        }
        return sb;
    }

    protected StringBuilder FreightTemplateBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable freightTemplateByLanguageVersion = this.ftm.GetFreightTemplateByLanguageVersion(this.Session["language_version"].ToString());
            foreach (DataRow row in freightTemplateByLanguageVersion.Rows)
            {
                builder.AppendLine(string.Concat(new object[] { "<option value=\"", row["id"], "\">", row["freightTemplateName"], "</option>" }));
            }
        }
        catch
        {
        }
        return builder;
    }

    protected StringBuilder GoodsTypeBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable goodsTypeByLanguageVersion = this.gtm.GetGoodsTypeByLanguageVersion(this.Session["language_version"].ToString());
            foreach (DataRow row in goodsTypeByLanguageVersion.Rows)
            {
                builder.AppendLine("<option value=\"" + row["id"].ToString() + "\">" + row["typeName"].ToString() + "</option>");
            }
        }
        catch
        {
        }
        return builder;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
        Utility.RegisterTypeForAjax(typeof(Goods_ajax));
        if (!base.IsPostBack)
        {
            this.rdIsBest0.Checked = true;
            this.rdIsNew0.Checked = true;
            this.rdIsTopSelling0.Checked = true;
            this.rdIsShelves1.Checked = true;
            this.rdIsPromote0.Checked = true;
            this.rdIsUserDiscount1.Checked = true;
            this.cbIsOrderFromFreight.Checked = true;
            this.cbIsDiscount.Checked = true;
            this.cbIsTotalReduce.Checked = true;
            this.cbIsGifts.Checked = true;
            this.cbIsSendIntegral.Checked = true;
            this.rdIsFromFreight0.Checked = true;
            this.txtGoodsName.Focus();
        }
    }

    private void RecursionCategory(StringBuilder sb, string parentCategory, string languageVersion)
    {
        DataTable table = this.cm.GetCategoryByParentIdAndLanguageVersionAndIsShow(parentCategory, languageVersion, -1);
        if (table.Rows.Count > 0)
        {
            foreach (DataRow row in table.Rows)
            {
                string str = "";
                int num = Convert.ToInt32(row["depth"]);
                if (num > 0)
                {
                    for (int i = 0; i < num; i++)
                    {
                        str = str + "&nbsp;&nbsp;&nbsp;&nbsp;";
                    }
                    str = str + "┣";
                }
                sb.AppendLine("<option value=\"" + row["categoryIdentity"].ToString() + "\">" + str + row["categoryName"].ToString() + "</option>");
                this.RecursionCategory(sb, row["id"].ToString(), languageVersion);
            }
        }
    }

    protected StringBuilder SpecificationsBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable specificationsByLanguageVersion = this.sm.GetSpecificationsByLanguageVersion(this.Session["language_version"].ToString());
            bool flag = false;
            foreach (DataRow row in specificationsByLanguageVersion.Rows)
            {
                builder.AppendLine("" + (!flag ? "" : "<br/>") + "<input id=\"cb" + row["id"].ToString() + "\" value=\"" + row["id"].ToString() + "\" name=\"ss\" type=\"checkbox\" onclick=\"getSpecifications(" + row["id"].ToString() + ",this);\" />[" + row["specificationsName"].ToString() + "]");
                flag = true;
            }
        }
        catch
        {
        }
        return builder;
    }

}